# Description:
# Mace libmace.
#
package(
    default_visibility = ["//visibility:public"],
)

licenses(["notice"])  # Apache 2.0

load(
    "//mace:mace.bzl",
    "apu_version_select",
    "if_android",
    "if_android_armv7",
    "if_apu_enabled",
    "if_bfloat16_enabled",
    "if_darwin",
    "if_fp16_enabled",
    "if_hexagon_enabled",
    "if_hexagon_or_hta_enabled",
    "if_cpu_enabled",
    "if_hta_enabled",
    "if_linux",
    "if_linux_base",
    "if_neon_enabled",
    "if_opencl_enabled",
    "if_quantize_enabled",
    "if_rpcmem_enabled",
)

cc_library(
    name = "libmace",
    srcs = glob(
        [
            "*.cc",
            "engines/*.cc",
        ],
        exclude = [
            "gpu_context_builder.cc",
        ],
    ) + if_opencl_enabled([
        "gpu_context_builder.cc",
    ]),
    hdrs = glob([
        "*.h",
        "engines/*.h",
    ]),
    copts = [
        "-Werror",
        "-Wextra",
    ] + if_neon_enabled([
        "-DMACE_ENABLE_NEON",
    ]) + if_android_armv7([
        "-mfpu=neon",
    ]) + if_android_armv7([
        "-mfloat-abi=softfp",
    ]) + if_opencl_enabled([
        "-DMACE_ENABLE_OPENCL",
    ]) + if_quantize_enabled([
        "-DMACE_ENABLE_QUANTIZE",
    ]) + if_bfloat16_enabled([
        "-DMACE_ENABLE_BFLOAT16",
    ]) + if_fp16_enabled([
        "-DMACE_ENABLE_FP16",
        "-march=armv8.2-a+fp16",
    ]) + if_hexagon_enabled([
        "-DMACE_ENABLE_HEXAGON",
    ]) + if_hta_enabled([
        "-DMACE_ENABLE_HTA",
    ]) + if_apu_enabled([
        "-DMACE_ENABLE_MTK_APU",
    ]) + if_rpcmem_enabled([
        "-DMACE_ENABLE_RPCMEM",
    ]) + apu_version_select(
        ["-DMACE_MTK_APU_VERSION=1"],
        ["-DMACE_MTK_APU_VERSION=2"],
        ["-DMACE_MTK_APU_VERSION=3"],
        ["-DMACE_MTK_APU_VERSION=4"],
    ),
    deps = [
        "//include:public_headers",
        "//mace/flows",
        "//mace/ops",
    ] + if_rpcmem_enabled([
        "//mace/rpcmems",
    ]),
    alwayslink = 1,
)

cc_binary(
    name = "libmace.so",
    linkopts = if_darwin(
        ["-Wl,-install_name,libmace.so"],
        [
            "-Wl,-soname,libmace.so",
            "-Wl,--version-script",
            "$(location //mace/libmace:mace_version_script.lds)",
        ],
    ),
    linkshared = 1,
    linkstatic = 0,
    deps = [
        "//mace/libmace",
        "//mace/libmace:mace_version_script.lds",
    ],
)

exports_files(
    [
        "mace_version_script.lds",
    ],
)

cc_library(
    name = "libmace_dynamic",
    srcs = ["libmace.so"],
    visibility = ["//visibility:public"],
)

# For details, see https://github.com/bazelbuild/bazel/issues/5200
genrule(
    name = "libmace_static",
    srcs = [
        "//include:public_headers",
        "//mace/codegen:generated_version",
        "//mace/core",
        "//mace/flows/cpu:cpu_flows",
        "//mace/flows",
        "//mace/runtimes/cpu:cpu_runtimes",
        "//mace/runtimes",
        "//mace/libmace",
        "//mace/port:port_base",
        "//mace/port/posix:port_posix",
        "//mace/utils",
        "//mace/proto:mace_cc",
        "@com_google_protobuf//:protobuf_lite",
        "//mace/ops:common",
    ] + if_linux_base([
        "//mace/port/linux_base:port_linux_base",
    ]) + if_android([
        "//mace/port/android:port_android",
    ]) + if_linux([
        "//mace/port/linux:port_linux",
    ]) + if_darwin([
        "//mace/port/darwin:port_darwin",
        "//mace/port/darwin:darwin_ar_merge",
    ]) + if_cpu_enabled([
        "//mace/ops:ref_kernels",
        "//mace/ops:internal_ops",
        "//mace/ops",
    ]) + if_opencl_enabled([
        "//mace/ops:opencl_kernels",
        "//mace/runtimes/opencl:opencl_runtimes",
        "//mace/codegen:generated_opencl",
        "//mace/flows/opencl:opencl_flows",
    ]) + if_hexagon_enabled([
        "//mace/runtimes/hexagon:dsp",
    ]) + if_hta_enabled([
        "//mace/runtimes/hexagon:hta",
    ]) + if_hexagon_or_hta_enabled([
        "//mace/runtimes/hexagon:base",
        "//mace/flows/hexagon:hexagon_flows",
        "//mace/runtimes/hexagon:hexagon_runtimes",
    ]) + if_apu_enabled([
        "//mace/flows/apu:apu_flows",
        "//mace/runtimes/apu:apu_runtimes",
    ]) + if_rpcmem_enabled([
        "//mace/rpcmems",
        "//third_party/rpcmem:rpcmem.a",
    ]) + if_neon_enabled([
        "//mace/ops:arm_neon_kernels",
    ]),
    outs = ["libmace.a"],
    cmd = "tmp_mri_file=$$(mktemp mace-static-lib-mri.XXXXXXXXXX);" +
          "mri_stream=$$(python $(location //mace/python/tools:archive_static_lib) " +
          "$(locations //mace/codegen:generated_version) " +
          "$(locations //mace/core:core) " +
          "$(locations //mace/flows/cpu:cpu_flows) " +
          "$(locations //mace/flows) " +
          "$(locations //mace/runtimes/cpu:cpu_runtimes) " +
          "$(locations //mace/runtimes) " +
          "$(locations //mace/ops:common) " +
          if_cpu_enabled(
            "$(locations //mace/ops:ref_kernels) " +
            "$(locations //mace/ops:internal_ops) " +
            "$(locations //mace/ops:ops) ",
            default_value = "",
          ) +
          if_neon_enabled(
              "$(locations //mace/ops:arm_neon_kernels) ",
              default_value = "",
          ) +
          if_opencl_enabled(
              "$(locations //mace/ops:opencl_kernels) " +
              "$(locations //mace/flows/opencl:opencl_flows) " +
              "$(locations //mace/runtimes/opencl:opencl_runtimes) ",
              default_value = "",
          ) + if_hexagon_or_hta_enabled(
              "$(locations //mace/runtimes/hexagon:base) " +
              "$(locations //mace/flows/hexagon:hexagon_flows) " +
              "$(locations //mace/runtimes/hexagon:hexagon_runtimes) ",
              default_value = "",
          ) +
          if_hexagon_enabled(
              "$(locations //mace/runtimes/hexagon:dsp) ",
              default_value = "",
          ) + if_hta_enabled(
              "$(locations //mace/runtimes/hexagon:hta) ",
              default_value = "",
          ) + if_apu_enabled(
              "$(locations //mace/flows/apu:apu_flows) " +
              "$(locations //mace/runtimes/apu:apu_runtimes) ",
              default_value = "",
          ) +
          "$(locations //mace/libmace:libmace) " +
          "$(locations //mace/port:port_base) " +
          "$(locations //mace/port/posix:port_posix) " +
          if_linux_base(
              "$(locations //mace/port/linux_base:port_linux_base) ",
              default_value = "",
          ) +
          if_android(
              "$(locations //mace/port/android:port_android) ",
              default_value = "",
          ) +
          if_linux(
              "$(locations //mace/port/linux:port_linux) ",
              default_value = "",
          ) +
          if_darwin(
              "$(locations //mace/port/darwin:port_darwin) ",
              default_value = "",
          ) +
          "$(locations //mace/utils:utils) " +
          "$(locations //mace/proto:mace_cc) " +
          "$(locations @com_google_protobuf//:protobuf_lite) " +
          if_opencl_enabled(
              "$(locations //mace/codegen:generated_opencl) ",
              default_value = "",
          ) +
          if_rpcmem_enabled(
              "$(locations //mace/rpcmems) " +
              "$(locations //third_party/rpcmem:rpcmem.a) ",
              default_value = "",
          ) +
          "$@ " +
          "$$tmp_mri_file);" +
          if_darwin(
              "bash $(locations //mace/port/darwin:darwin_ar_merge) $(AR) < $$tmp_mri_file;",
              default_value = "$(AR) -M <$$tmp_mri_file;",
          ) +
          "rm -rf $$tmp_mri_file;",
    # "$(STRIP) -x $@;",  # FIXME this will crash
    tools = ["//mace/python/tools:archive_static_lib"],
    visibility = ["//visibility:public"],
)
